package study;

import study.远程调用.entity.AcquisitionPoint;

import java.util.*;

/**
 * @Author 刘伟涛
 * @Date 2024/6/14 16:59
 **/
public class 统计集合重复次数 {
    public static void main(String[] args) {
        List<AcquisitionPoint> acquisitionPoints = new ArrayList<>();
        AcquisitionPoint acquisitionPoint1 = new AcquisitionPoint();
        acquisitionPoint1.setGatherKey("2");
        AcquisitionPoint acquisitionPoint2 = new AcquisitionPoint();
        acquisitionPoint2.setGatherKey("2");
        AcquisitionPoint acquisitionPoint3 = new AcquisitionPoint();
        acquisitionPoint3.setGatherKey("2");
        AcquisitionPoint acquisitionPoint4 = new AcquisitionPoint();
        acquisitionPoint4.setGatherKey("4");
        acquisitionPoints.add(acquisitionPoint1);
        acquisitionPoints.add(acquisitionPoint2);
        acquisitionPoints.add(acquisitionPoint3);
        acquisitionPoints.add(acquisitionPoint4);
        // 创建一个 HashMap 来统计出现次数
        Map<String, Integer> frequencyMap = new HashMap<>();

        for (AcquisitionPoint acquisitionPoint : acquisitionPoints) {
            frequencyMap.put(acquisitionPoint.getGatherKey(), frequencyMap.getOrDefault(acquisitionPoint.getGatherKey(), 0) + 1);
        }
//        去除重复，
        Set<AcquisitionPoint> set = new HashSet<>();
        set.addAll(acquisitionPoints);
        System.out.println("去重后的list集合" + set);

        // 打印结果
        for (Map.Entry<String, Integer> entry : frequencyMap.entrySet()) {
            System.out.println("Item: " + entry.getKey() + ", Count: " + entry.getValue());
        }
    }
}
